/*
 * $Id: AutoscWhiteListDAO.java,v 1.4 2012/02/29 18:22:06 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.CfgAutoscWhitelist;

/**
 * @author 212042764
 * 
 */
public class AutoscWhiteListDAO extends BaseDAO implements IAutoscWhiteListDAO {
	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -5013400677895789063L;

	private static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.CfgAutoscWhitelist";

	private static final String COL_EMAIL_ID = "emailId";
	private static final String COL_EMAIL_TYPE = "emailType";
	private static final String COL_REQEST_DEFN_ID = "requestDefnId";
	private static final String COL_IS_ALLOWED = "isAllowed";

	protected AutoscWhiteListDAO() {
		super(TABLE_NAME);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IAutoscWhiteListDAO#getValidEmailIds()
	 */
	public List<String> getValidEmailIds() {
		Map<String, Object> wc = new HashMap<String, Object>();
		wc.put("upper(" + COL_IS_ALLOWED + ")", "Y");
		wc.put(COL_EMAIL_TYPE, CfgAutoscWhitelist.EMAIL_TYPE);
		List<?> rtvdLst = findByProperties(wc);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			return null;
		}
		List<String> validEmailIds = new ArrayList<String>();
		for (Object whiteLstItem : rtvdLst) {
			CfgAutoscWhitelist item = (CfgAutoscWhitelist) whiteLstItem;
			validEmailIds.add(item.getEmailId());
		}
		return validEmailIds;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IAutoscWhiteListDAO#isWhiteListedEmail(java.lang.String)
	 */
	public boolean isWhiteListedEmail(String email) {
		boolean isWhiteList = true;
		if (ASCUtil.isNullOrEmpty(email)) {
			isWhiteList = false;
		}
		Map<String, Object> wc = new HashMap<String, Object>();
		wc.put(COL_EMAIL_ID, email);
		wc.put("upper(" + COL_IS_ALLOWED + ")", "Y");
		wc.put(COL_EMAIL_TYPE, CfgAutoscWhitelist.EMAIL_TYPE);
		List<?> rtvdLst = findByProperties(wc);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			isWhiteList = false;
		}
		return isWhiteList;
	}

	public boolean isWhiteListedEmail(Long rqDefnId, String email) {
		boolean isWhiteList = true;
		if (ASCUtil.isNullOrEmpty(email)) {
			isWhiteList = false;
		}
		Map<String, Object> wc = new HashMap<String, Object>();
		wc.put(COL_EMAIL_ID, email);
		wc.put("upper(" + COL_IS_ALLOWED + ")", "Y");
		wc.put(COL_EMAIL_TYPE, CfgAutoscWhitelist.EMAIL_TYPE);
		wc.put(COL_REQEST_DEFN_ID, rqDefnId);
		List<?> rtvdLst = findByProperties(wc);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			isWhiteList = false;
		}
		return isWhiteList;
	}

	public boolean isUserValid(Long rqDefnId, String sso) {
		boolean isWhiteList = true;
		if (ASCUtil.isNullOrEmpty(sso)) {
			isWhiteList = false;
		}
		Map<String, Object> wc = new HashMap<String, Object>();
		wc.put(COL_EMAIL_ID, sso);
		wc.put("upper(" + COL_IS_ALLOWED + ")", "Y");
		wc.put(COL_EMAIL_TYPE, CfgAutoscWhitelist.SSO_TYPE);
		wc.put(COL_REQEST_DEFN_ID, rqDefnId);
		List<?> rtvdLst = findByProperties(wc);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			isWhiteList = false;
		}
		return isWhiteList;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IAutoscWhiteListDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		this.save(pojo);

	}

	public boolean isValid(ActRequest request, ActRequestListUser user) {
		boolean isValid = true;
		if (ASCUtil.isNullOrEmptyObject(request)) {
			isValid = false;
		} else if (ASCUtil.isNullOrEmptyObject(user)) {
			isValid = false;
		} else if (request.getRequestDefnId() == null) {
			isValid = false;
		} else if (!isControlledRequestDefn(request.getRequestDefnId())) {
			isValid = true;
		} else if (user.getSso() == null) {
			// check to see if white list
			if (ASCUtil.isNullOrEmpty(user.getEmailAddress())) {
				isValid = false;
			} else {
				isValid = isWhiteListedEmail(request.getRequestDefnId(),
						user.getEmailAddress());
			}
		} else if (isUserValid(request.getRequestDefnId(),
				user.getSsoInString())) {
			isValid = true;
		} else {
			isValid = false;
		}
		return isValid;
	}

	public boolean isControlledRequestDefn(Long requestDefnId) {
		if (requestDefnId == null) {
			return false;
		}
		Map<String, Object> wc = new HashMap<String, Object>();
		wc.put(COL_REQEST_DEFN_ID, requestDefnId);
		wc.put("upper(" + COL_IS_ALLOWED + ")", "Y");
		wc.put(COL_EMAIL_TYPE, CfgAutoscWhitelist.SSO_TYPE);
		List<?> rtvdLst = findByProperties(wc);
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			return false;
		}
		return true;
	}

}
